package gui;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import gestionBDD.accesBDD;
import gestionBDD.requetesTirage;
import interne.* ;
import com.trolltech.qt.gui.*;


public class fenetreTirageEdition extends QWidget {
	
	private Tirage tirage ;
    private QLineEdit prixLineEdit ;

	public fenetreTirageEdition(Tirage t) {
		
		setTirage(t);
		        
		/** Chemin's GroupBox' **/
        QGroupBox tirageGroup = new QGroupBox(tr("Tirage"));
        
        /* Chemin Line Edit with Label */
        QLabel libelleLabel = new QLabel(tr("Libelle {Photo|Album} :"));
        QLabel libelleLineEdit = new QLabel(tr(tirage.getLibelle()));

        /* Chemin Line Edit with Label */
        QLabel prixLabel = new QLabel(tr("Prix unitaire :"));
        prixLineEdit = new QLineEdit(tirage.getPrixUnitaire().toString());
        
        /* Ok Button Box */
        QDialogButtonBox boutonOk = new QDialogButtonBox();
        boutonOk.addButton(QDialogButtonBox.StandardButton.Ok);
        boutonOk.clicked.connect(this, "updateTirage()");
        
        /* Chemin's Layout */
        QGridLayout tirageLayout = new QGridLayout();
        tirageLayout.addWidget(libelleLabel, 0, 0);
        tirageLayout.addWidget(libelleLineEdit, 0, 1);
        tirageLayout.addWidget(prixLabel, 1, 0) ;
        tirageLayout.addWidget(prixLineEdit, 1, 1) ;
        tirageLayout.addWidget(boutonOk, 2, 1) ;
        
        tirageGroup.setLayout(tirageLayout);
        
        setWindowTitle(tr("Nouveau Tirage"));
        setLayout(tirageLayout);
	}
	
	public void updateTirage() throws SQLException {
		try {
			Tirage t = new Tirage(tirage.getLibelle(), Float.parseFloat(prixLineEdit.text())) ;
			requetesTirage.modifierTirage(t) ;
			this.hide();
    	}
		catch(SQLException e) {
			System.out.println(e);
		}
	}
	
	public void supprimerTirage(Tirage t) throws SQLException {
		try {
			String sql = "DELETE FROM TIRAGE where libelle = ?" ;
			PreparedStatement statement = accesBDD.getInstance().prepareStatement(sql);
			statement.setObject(1, t.getLibelle());
			statement.executeUpdate();
			statement.close();			
			accesBDD.getInstance().commit();
		}
		catch(SQLException e) {
			System.out.println(e) ;
		}
	}

	public Tirage getTirage() {
		return tirage;
	}

	public void setTirage(Tirage tirage) {
		this.tirage = tirage;
	}
	
}
